﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SVC_BLL;

namespace SVC.MENUS
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void btEntrar_Click(object sender, EventArgs e)
        {
            try
            {
                txtLogin.Text = txtLogin.Text.Trim();
                txtSenha.Text = txtSenha.Text.Trim();
                if (txtLogin.Text != "" && txtSenha.Text != "")
                {
                    UsuarioBLL objUsu = new UsuarioBLL();
                    objUsu.localizarLogin(txtLogin.Text,txtSenha.Text);
                    if (objUsu.usu_codigo != 0)
                    {
                        if (objUsu.usu_status != "Ativo")
                            throw new Exception("O usuário está inativo");
                        global.codUsuario = objUsu.usu_codigo;
                        global.nomeUsuario = objUsu.usu_nome;
                        PermissaoBLL objPer = new PermissaoBLL();
                        objPer.per_codigo = objUsu.per_codigo;
                        objPer.localizar(objUsu.per_codigo.ToString(), "per_codigo");
                        global.permissao = objPer.per_nome;

                        this.Hide();
                        //globalDAL.con = cbAcessoBanco.SelectedItem.ToString();
                        Menu frm = new Menu();
                        frm.ShowDialog();
                        frm = null;
                        this.Show();
                    }
                    else
                        throw new Exception("Usuário não existente");

                }
                else
                    throw new Exception("Favor, preencha o campo 'Login' e 'Senha' com um usuário válido para poder logar");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void btSair_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        private void txtLogin_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
                btEntrar_Click(sender, e);
        }

        private void txtSenha_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((Keys)e.KeyChar == Keys.Enter)
                btEntrar_Click(sender, e);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Escape) 
                this.Close();
            return base.ProcessCmdKey(ref msg, keyData);
        } 
    }
}
